import axios from "axios";

//真机获取
axios.defaults.adapter = function (config) {
  return new Promise((resolve, reject) => {
    console.log(config);
    var settle = require("axios/lib/core/settle");
    var buildURL = require("axios/lib/helpers/buildURL");
    uni.request({
      method: config.method.toUpperCase(),
      url: buildURL(config.url, config.params, config.paramsSerializer),
      header: config.headers,
      data: config.data,
      dataType: config.dataType,
      responseType: config.responseType,
      sslVerify: config.sslVerify,
      complete: function complete(response) {
        response = {
          data: response.data,
          status: response.statusCode,
          errMsg: response.errMsg,
          header: response.header,
          config: config,
        };

        settle(resolve, reject, response);
      },
    });
  });
};
// 创建axios实例
const service = axios.create({
  // axios中请求配置有baseURL选项，表示请求URL公共部分
  // baseURL: process.env.VUE_APP_BASE_API,
  baseURL: "",
  // 超时（毫秒）
  timeout: 5000,
});
// 请求拦截
axios.interceptors.request.use(
  (config) => {
    uni.showLoading({
      title: "加载中",
    });
    return config;
  },
  (error) => {
    uni.showLoading({
      title: "加载中",
    });
    Promise.reject(error);
  }
);
//响应拦截
axios.interceptors.response.use(
  (res) => {
    uni.hideLoading();
    return res;
  },
  (error) => {
    uni.hideLoading();
    return Promise.reject(error);
  }
);

export default service;
